package org.szgb.console.sys.equipment.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.sys.line.entity.SysLine;
import org.szgb.console.sys.system.entity.SysSystem;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysEquipment extends BeanEntity<SysEquipment> {
    @Serial
    private static final long serialVersionUID = 1L;
    private SysLine line;
    private Integer type;
    private SysSystem system;
    private String manufacturer;
    private String name;

    public SysEquipment() {
    }

    public SysEquipment(String id) {
        this.setId(id);
    }

    public SysEquipment(SysEquipment.SysEquipmentFindDto sysEquipmentFindDto) {
        this.setId(sysEquipmentFindDto.getId());
        this.setLine(new SysLine(sysEquipmentFindDto.getLineId()));
        this.setType(sysEquipmentFindDto.getType());
        this.setSystem(new SysSystem(sysEquipmentFindDto.getSystemId()));
        this.setManufacturer(sysEquipmentFindDto.getManufacturer());
        this.setName(sysEquipmentFindDto.getName());
        this.setPageDto(sysEquipmentFindDto.getPageDto());
    }

    public SysEquipment(SysEquipment.SysEquipmentSaveDto sysEquipmentSaveDto) {
        this.setId(sysEquipmentSaveDto.id);
        this.setLine(new SysLine(sysEquipmentSaveDto.getLineId()));
        this.setType(sysEquipmentSaveDto.getType());
        this.setSystem(new SysSystem(sysEquipmentSaveDto.getSystemId()));
        this.setManufacturer(sysEquipmentSaveDto.getManufacturer());
        this.setName(sysEquipmentSaveDto.name);
    }

    public SysEquipment.SysEquipmentView toSysEquipmentView() {
        return new SysEquipment.SysEquipmentView(this);
    }

    @Data
    @Schema(title = "设备信息", description = "设备信息")
    public static class SysEquipmentView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "使用线路id")
        private String lineId;
        @Schema(description = "使用线路名称")
        private String lineName;
        @Schema(description = "设备类型（1：正线、2：主所）")
        private Integer type;
        @Schema(description = "系统id")
        private String systemId;
        @Schema(description = "系统名称")
        private String systemName;
        @Schema(description = "厂家")
        private String manufacturer;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysEquipmentView(SysEquipment sysEquipment) {
            this.id = sysEquipment.getId();
            this.lineId = sysEquipment.getLine().getId();
            this.lineName = sysEquipment.getLine().getName();
            this.type = sysEquipment.getType();
            this.systemId = sysEquipment.getSystem().getId();
            this.systemName = sysEquipment.getSystem().getName();
            this.manufacturer = sysEquipment.getManufacturer();
            this.name = sysEquipment.getName();
            this.createdAt = sysEquipment.getCreatedAt();
            this.updatedAt = sysEquipment.getUpdatedAt();
            if (sysEquipment.getCreatedBy() != null) {
                this.createdById = sysEquipment.getCreatedBy().getId();
                this.createdByName = sysEquipment.getCreatedBy().getName();
            }
            if (sysEquipment.getUpdatedBy() != null) {
                this.updatedById = sysEquipment.getUpdatedBy().getId();
                this.updatedByName = sysEquipment.getUpdatedBy().getName();
            }
        }

        public static List<SysEquipment.SysEquipmentView> getViewListFromList(List<SysEquipment> sysEquipmentList) {
            return sysEquipmentList.stream().map(SysEquipment.SysEquipmentView::new).toList();
        }
    }

    @Data
    @Schema(title = "设备查询表单", description = "设备查询参数")
    public static class SysEquipmentFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "使用线路id")
        private String lineId;
        @Schema(description = "设备类型（1：正线、2：主所）")
        private Integer type;
        @Schema(description = "使用系统id")
        private String systemId;
        @Schema(description = "厂家")
        private String manufacturer;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysEquipment toSysEquipment() {
            return new SysEquipment(this);
        }
    }

    @Data
    @Schema(title = "设备保存表单", description = "设备保存参数")
    public static class SysEquipmentSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "使用线路id")
        private String lineId;
        @Schema(description = "设备类型（1：正线、2：主所）")
        private Integer type;
        @Schema(description = "使用系统id")
        private String systemId;
        @Schema(description = "厂家")
        private String manufacturer;
        @Schema(description = "名称")
        private String name;

        public SysEquipment toSysEquipment() {
            return new SysEquipment(this);
        }
    }
}
